package com.howei.controller;

import com.howei.pojo.InverterStatus;
import com.howei.pojo.MachineExternalState;
import com.howei.pojo.MotorProtectorStatus;
import com.howei.service.DataSummaryService;
import com.howei.util.LayuiResult;
import com.howei.util.ResultEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("${baseURL.prefix}/dataSummary")
public class DataSummaryController {

    @Autowired
    private DataSummaryService dataSummaryService;

    /**
     * 跳转现场数据汇总页
     *
     * @return
     */
    @RequestMapping("/toDataSummary")
    public String toDataSummary() {
        return "dataSummary";
    }


    /**
     * @param type  0循环泵马达保护器,1循环泵变频器,2其他马达保护器
     * @param name  查询名称
     * @param page  页数
     * @param limit 每页大小
     * @return
     */
    @RequestMapping("/getDataSummaryList")
    @ResponseBody
    public LayuiResult getDataSummary(
            @RequestParam(required = false) String type,
            @RequestParam(required = false) String name,
            @RequestParam(required = false) Integer page,
            @RequestParam(required = false) Integer limit
    ) {

        Map<String, Object> map = new HashMap<>();
        if (page != null && limit != null) {
            map.put("page", (page - 1) * limit);
            map.put("limit", limit);
        }
        if (StringUtils.isEmpty(name)) {
            return LayuiResult.fail(ResultEnum.NO_PARAMETERS);
        }
        if ("0".equals(type)) {
            String motorName = "Power_frequency_circuit_of_circulating_pump_" + name;
            map.put("name", motorName);
            List<MotorProtectorStatus> motorProtectorStatuses = dataSummaryService.getMotorProtectorStatusByMap(map);
            List<MachineExternalState> machineExternalStateList = dataSummaryService.getMachineExternalStateByMap(map);
            for (int i = 0; i < motorProtectorStatuses.size(); i++) {
                if ("A".equals(name)) {
                    motorProtectorStatuses.get(i).setVibrMotor(machineExternalStateList.get(i).getVibr2());
                    motorProtectorStatuses.get(i).setVibrPump(machineExternalStateList.get(i).getVibr1());
                    motorProtectorStatuses.get(i).setTemperature(machineExternalStateList.get(i).getTemp4());
                } else if ("B".equals(name)) {
                    motorProtectorStatuses.get(i).setVibrMotor(machineExternalStateList.get(i).getVibr3());
                    motorProtectorStatuses.get(i).setVibrPump(machineExternalStateList.get(i).getVibr4());
                    motorProtectorStatuses.get(i).setTemperature(machineExternalStateList.get(i).getTemp1());

                } else if ("C".equals(name)) {
                    motorProtectorStatuses.get(i).setVibrMotor(machineExternalStateList.get(i).getVibr7());
                    motorProtectorStatuses.get(i).setVibrPump(machineExternalStateList.get(i).getVibr5());
                    motorProtectorStatuses.get(i).setTemperature(machineExternalStateList.get(i).getTemp2());
                } else if ("D".equals(name)) {
                    motorProtectorStatuses.get(i).setVibrMotor(machineExternalStateList.get(i).getVibr8());
                    motorProtectorStatuses.get(i).setVibrPump(machineExternalStateList.get(i).getVibr6());
                    motorProtectorStatuses.get(i).setTemperature(machineExternalStateList.get(i).getTemp3());
                }
            }
            return LayuiResult.ok(motorProtectorStatuses.size(), motorProtectorStatuses);

        }
        if ("1".equals(type)) {
            String equipmentName = "Frequency_converter_" + name;
            map.put("name", equipmentName);
            List<InverterStatus> inverterStatusList = dataSummaryService.getInverterStatusListByMap(map);
            List<MachineExternalState> machineExternalStateList = dataSummaryService.getMachineExternalStateByMap(map);

            for (int i = 0; i < inverterStatusList.size(); i++) {
                if ("A".equals(name)) {
                    inverterStatusList.get(i).setVibrMotor(machineExternalStateList.get(i).getVibr2());
                    inverterStatusList.get(i).setVibrPump(machineExternalStateList.get(i).getVibr1());
                    inverterStatusList.get(i).setTemperature(machineExternalStateList.get(i).getTemp4());
                } else if ("B".equals(name)) {
                    inverterStatusList.get(i).setVibrMotor(machineExternalStateList.get(i).getVibr3());
                    inverterStatusList.get(i).setVibrPump(machineExternalStateList.get(i).getVibr4());
                    inverterStatusList.get(i).setTemperature(machineExternalStateList.get(i).getTemp1());

                } else if ("C".equals(name)) {
                    inverterStatusList.get(i).setVibrMotor(machineExternalStateList.get(i).getVibr7());
                    inverterStatusList.get(i).setVibrPump(machineExternalStateList.get(i).getVibr5());
                    inverterStatusList.get(i).setTemperature(machineExternalStateList.get(i).getTemp2());
                } else if ("D".equals(name)) {
                    inverterStatusList.get(i).setVibrMotor(machineExternalStateList.get(i).getVibr8());
                    inverterStatusList.get(i).setVibrPump(machineExternalStateList.get(i).getVibr6());
                    inverterStatusList.get(i).setTemperature(machineExternalStateList.get(i).getTemp3());
                }
            }
            return LayuiResult.ok(inverterStatusList.size(), inverterStatusList);
        }
        map.put("name", name);
        List<MotorProtectorStatus> motorProtectorStatuses = dataSummaryService.getMotorProtectorStatusByMap(map);

        return LayuiResult.ok(motorProtectorStatuses.size(), motorProtectorStatuses);
    }
}
